<?php
/**
 * 栏目管理（栏目的增删改查等操作）
 * User: Backli
 * Date: 12-11-28
 * Time: 下午4:03
 */
class CategoryAction extends CommonAction{
    /*
     * 默认显示栏目列表
     */
    public function index(){
        $category = M("category");
        $data = $category->select();
        //Volist 标签 TP31手册8.9
        $this->category = $data;
        $this->display();
    }
    /*
     * 栏目添加
     */
    public function add(){
        $cate = D("Category");
        //判断是否来自于form表单的数据添加请求
        if($_POST){
            if($cate->create()){
                $result = $cate->add();
                if($result){
                    $this->success("操作成功！",__URL__."/");
                }else{
                    $this->error("写入错误".$cate->getError());
                }
            }
        }else{//默认需要获取栏目列表在所属栏目的位置
            $data = $cate->select();
            $this->category = $data;
            $this->display();
        }
    }
    /*
     * 删除栏目
     */
    public function delete(){
        $id = $_GET['id'];
        if($id){
            $cate = D("Category");
            $result = $cate->delete($id);
            if($result>0){
                $this->success("删除成功，id编号为：".$id." ; 共删除".$result."条记录",null,10);
            }else{
                $this->error("操作失败，".$cate->getError());
            }
        }else{
            $this->error("无参数操作");
        }
    }
    /*
     * 修改栏目
     */
    public function edit(){
        $cate = D("Category");
        if($_POST){//判断数据是否来自POST表单
             if($cate->create()){
                 $result = $cate->save();
                 if($result){
                     $this->success("修改成功",__URL__."/");
                 }else{
                     $this->error("操作失败");
                 }
             }
        }else{
            $id = $_GET['id'];
            if(!$id){
                $this->error("非法操作");
            }
            $category = $cate->select();
            $singleCate = $cate->find($id);
            if($singleCate){
                $this->category = $category;
                $this->singleCate = $singleCate;
                $this->display();
            }else{
                $this->error("非法参数",__URL__."/");
            }
        }
    }
}
